package com.hjm.project.area.controller;


import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.area.domain.Area;
import com.hjm.project.area.service.IAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * areaController
 * 
 * @author LOVE
 * @date 2020-10-18
 */
@Controller
@RequestMapping("/area/area")
public class AreaController extends BaseController
{
    private String prefix = "area/area";

    @Autowired
    private IAreaService areaService;

    @RequiresPermissions("area:area:view")
    @GetMapping()
    public String area()
    {
        return prefix + "/area";
    }

    /**
     * 查询area列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Area area)
    {
        startPage();
        List<Area> list = areaService.selectAreaList(area);
        return getDataTable(list);
    }



    /**
     * 查询地区信息
     * @return Json
     * @throws Exception e
     */
    @RequestMapping("/findQBArea")
    public @ResponseBody List<String[]> findQBArea(@RequestParam String index, @RequestParam String area_type) throws Exception{
        List<String[]> dataList=new ArrayList<>();
        dataList.add(new String[]{"0","请选择"});
        List<Map<String,Object>> list=areaService.findQBArea(index, area_type);
        for(int i=0;i<list.size();i++){
            Map<String,Object> data=list.get(i);
            String[] tree=new String[2];
            tree[0]=data.get("AREA_ID").toString();
            tree[1]=data.get("AREA_NAME").toString();
            dataList.add(tree);
        }
        return dataList;
    }



    /**
     * 导出area列表
     */
    @RequiresPermissions("area:area:export")
    @Log(title = "area", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Area area)
    {
       /* List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        return util.exportExcel(list, "area");*/
        return null;
    }

    /**
     * 新增area
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存area
     */
    @RequiresPermissions("area:area:add")
    @Log(title = "area", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Area area)
    {
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改area
     */
    @GetMapping("/edit/{areaId}")
    public String edit(@PathVariable("areaId") Long areaId, ModelMap mmap)
    {
        Area area = areaService.selectAreaById(areaId);
        mmap.put("area", area);
        return prefix + "/edit";
    }

    /**
     * 修改保存area
     */
    @RequiresPermissions("area:area:edit")
    @Log(title = "area", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Area area)
    {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除area
     */
    @RequiresPermissions("area:area:remove")
    @Log(title = "area", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(areaService.deleteAreaByIds(ids));
    }
}
